//
//  BallAnimation.m
//  FrogDrying
//
//  Created by Le Thanh Hai on 4/5/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "BallAnimation.h"

@implementation BallAnimation
@synthesize colour;
@synthesize delegate;

-(id)init
{
    if (self = [super init]) {        
        colour = arc4random() % TotalColor + 1;
        [self initWithColor:colour];
    }
    return self;
}

-(id)initWithColor:(ColorType)colour_
{
    if (self = [super init]) {        
        colour = colour_;
        CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
        name = @"";
        switch (colour) {
            case COLOR_BLUE:
                name = @"ball_blue_0%d.png";
                break;
                
            case COLOR_CYAN:
                name = @"ball_cyan_0%d.png";
                break;
                
            case COLOR_ALL:
                name = @"ball_free_0%d.png";
                break;
                
            case COLOR_GREEN:
                name = @"ball_green_0%d.png";
                break;
                
            case COLOR_PINK:
                name = @"ball_pink_0%d.png";
                break;
                
            case COLOR_RED:
                name = @"ball_red_0%d.png";
                break;
                
            case COLOR_WHITE:
                name = @"ball_white_0%d.png";
                break;
                
            default:
                break;
        }
        
        CCSprite *shadow = [CCSprite spriteWithSpriteFrame:[cache spriteFrameByName:@"dropshadow_ball.png"]];
        shadow.anchorPoint = ccp(0.5, 1.0);
        shadow.position = ccp(40.5, 0);
        [self addChild:shadow];
        
        NSMutableArray *balls = [[[NSMutableArray alloc] init] autorelease];
        for (int i = 1; i <= 3; ++i) {
            CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:name, i]];
            [balls addObject:frame];
        }
        float rand = arc4random() % 3 + 3;
        ballAnimation = [[CCAnimation alloc] initWithSpriteFrames:balls delay:1.0/rand];
        CCAnimate *animation = [CCAnimate actionWithAnimation:ballAnimation];
        [self runAction:[CCRepeatForever actionWithAction:[CCSequence actions:animation, [animation reverse], nil]]];
        
        NSMutableArray *explosion = [[[NSMutableArray alloc] init] autorelease];
        for (int i = 4; i <= 7; ++i) {
            CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:name, i]];
            [explosion addObject:frame];
        }
        explosionAnimation = [[CCAnimation alloc] initWithSpriteFrames:explosion delay:1.0/30.0];
    }
    return self;
}

-(void)resetBall
{
    if (frog) {
        [frog removeFromParentAndCleanup:YES];
        frog = nil;
    }
    CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
    NSMutableArray *balls = [[[NSMutableArray alloc] init] autorelease];
    colour = arc4random() % TotalColor + 1;
    name = @"";
    switch (colour) {
        case COLOR_BLUE:
            name = @"ball_blue_0%d.png";
            break;
            
        case COLOR_CYAN:
            name = @"ball_cyan_0%d.png";
            break;
            
        case COLOR_ALL:
            name = @"ball_free_0%d.png";
            break;
            
        case COLOR_GREEN:
            name = @"ball_green_0%d.png";
            break;
            
        case COLOR_PINK:
            name = @"ball_pink_0%d.png";
            break;
            
        case COLOR_RED:
            name = @"ball_red_0%d.png";
            break;
            
        case COLOR_WHITE:
            name = @"ball_white_0%d.png";
            break;
            
        default:
            break;
    }
    for (int i = 1; i <= 3; ++i) {
        CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:name, i]];
        [balls addObject:frame];
    }
    float rand = arc4random() % 3 + 3;
    ballAnimation = [[CCAnimation alloc] initWithSpriteFrames:balls delay:1.0/rand];
    [self stopAllActions];
    CCAnimate *animation = [CCAnimate actionWithAnimation:ballAnimation];
    [self runAction:[CCRepeatForever actionWithAction:[CCSequence actions:animation, [animation reverse], nil]]];
    
    NSMutableArray *explosion = [[[NSMutableArray alloc] init] autorelease];
    for (int i = 4; i <= 7; ++i) {
        CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:name, i]];
        [explosion addObject:frame];
    }
    explosionAnimation = [[CCAnimation alloc] initWithSpriteFrames:explosion delay:1.0/30.0];
}

-(void)explosion
{
    [self stopAllActions];
    [self runAction:[CCSequence actions:[CCAnimate actionWithAnimation:explosionAnimation], [CCCallBlock actionWithBlock:^{
        if (delegate != nil && [delegate respondsToSelector:@selector(ballExplosion:)]) {
            [delegate ballExplosion:self];
        }
    }], nil]];
}

-(BOOL)containsTouchLocation:(CGPoint)postion
{
    return CGRectContainsPoint([self rect], postion);
}

-(void) onEnter
{
	CCDirector *director = [CCDirector sharedDirector];
	[[director touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
    [super onEnter];
}

-(void) onExit
{    
	CCDirector *director = [CCDirector sharedDirector];
	[[director touchDispatcher] removeDelegate:self];
    [super onExit];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    CGPoint convLoc = [[CCDirector sharedDirector] convertToGL:location];
    if ([self containsTouchLocation:convLoc]) {
        [self explosion];
    }
    return YES;
}

-(void)addFrogWith:(ColorType)type
{
    CGSize size = self.contentSize;
    CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
    NSMutableArray *frogAnimation = [[[NSMutableArray alloc] init] autorelease];
    
    for (int i = 2; i <= 4; ++i) {
        CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:@"hove_1_0%d.png", i]];
        [frogAnimation addObject:frame];
    }
    
    CCAnimation *frogs = [[CCAnimation alloc] initWithSpriteFrames:frogAnimation delay:1.0/5];
    CCAnimate *animation = [CCAnimate actionWithAnimation:frogs];
    
    frog = [[CCSprite alloc] init];
    frog.position = ccp(size.width*0.5, size.height*0.5);
    [frog runAction:[CCRepeatForever actionWithAction:[CCSequence actions:animation, [animation reverse], nil]]];
    [self addChild:frog z:-100];
}

-(CGRect)rect {
    CGPoint point = self.position;
    float w = self.contentSize.width;
    float h = self.contentSize.height;
    CGRect rect = CGRectMake(point.x - w*0.5, point.y - h*0.5, w, h);
    return rect;
}
@end
